package com.fingard.dsp.web.page.extsys.notify;

//import java.net.URLDecoder;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.web.page.PageBase;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

import java.io.File;
import java.util.Date;

public class BhLife  extends PageBase {
	
	@Override
	public void pageLoad() throws Exception{
		StringBuilder sbResp = new StringBuilder();
		ReqRespHeader header = new ReqRespHeader();
		header.bankCode="BhLife";
		header.transType="notify";
		
		try{
			httpContext.request.httpReceiver.setCharset("utf-8");
			String reqBody = httpContext.request.httpReceiver.getReqBodyContent();
			//reqBody = URLDecoder.decode(reqBody,FGBiz.getDefaultCharset());
			//reqBody = reqBody.replace("reqDataXml=", "");
			Date notifyDate = new Date();
			LogHelper.writeBankLogByHour(notifyDate, "", header, "接收到的请求:\r\n" + reqBody);
			if(reqBody!=null && reqBody.contains("<TransData>") && reqBody.contains("reimburseMessage")){
				int startIdx = reqBody.indexOf("<LCCont>");
				int endIdx = reqBody.lastIndexOf("</LCCont>");
				if(startIdx>=0 && endIdx>startIdx){
					String pickTxt = reqBody.substring(startIdx, endIdx+9)+"\r\n";
                    String tmpDate = Format.Date8Format.format(notifyDate);
                    String tmpByDate = tmpDate.substring(0, 6) + File.separator + tmpDate.substring(6, 8);
                    String dataFilePath = FGBiz.dspCenter.dspSet.dataDirPath + File.separator + "datafile" + File.separator + tmpByDate + File.separator + "BhLife01"+File.separator+tmpDate+"_notify.txt";
					LogHelper.writeBankLogByHour(new Date(), "", header, "提取数据保存至:" + dataFilePath);
					FileHelper.appendFile(dataFilePath, pickTxt, FGBiz.getDefaultCharset());
				}else{
					LogHelper.writeBankLogByHour(new Date(), "", header, "未能提取到有效数据<LCCont>...</LCCont>");
				}
				
			}else{
				LogHelper.writeBankLogByHour(new Date(), "", header, "未知的transType");
			}
			String PolicyNum="";
			String EdorAcceptNo="";
			if(StringHelper.hasAnyChar(reqBody)){
				XmlTextReader xr = new XmlTextReader(reqBody);
				PolicyNum = xr.getSimpleText("PolicyNum");
				EdorAcceptNo = xr.getSimpleText("EdorAcceptNo");
			}else{
				LogHelper.writeBankLogByDay(new Date(), "", header, "接收到的原始请求:\r\n" + httpContext.request.httpReceiver.getRawReqStr());
			}
			httpContext.response.isRtnBytes = false;
			httpContext.response.setContentType("txt", "");

			
			sbResp.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
			sbResp.append("<TransData>\r\n");
			sbResp.append("  <InputData>\r\n");
			sbResp.append("    <EdorChangeNo>"+PolicyNum+"</EdorChangeNo>\r\n");
			sbResp.append("    <EdorAcceptNo>"+EdorAcceptNo+"</EdorAcceptNo>\r\n");
			if(StringHelper.hasAnyChar(PolicyNum) && StringHelper.hasAnyChar(EdorAcceptNo)){
				sbResp.append("    <resultCode>0</resultCode>\r\n");
				sbResp.append("    <resultMsg>成功</resultMsg>\r\n");
			}else{
				sbResp.append("    <resultCode>1</resultCode>\r\n");
				sbResp.append("    <resultMsg>PolicyNum或EdorAcceptNo都不能为空</resultMsg>\r\n");
			}
			sbResp.append("  </InputData>\r\n");
			sbResp.append("</TransData>\r\n");
			httpContext.response.respMsg = sbResp.toString();
			LogHelper.writeBankLogByHour(new Date(), "", header, "返回的响应:\r\n" + httpContext.response.respMsg);
		}catch(Exception ex){
			LogHelper.writeBankLogByHour(new Date(), "", header, LogHelper.getStackTrace(ex));
			if(StringHelper.isNullOrEmpty(httpContext.response.respMsg)){
				httpContext.response.respMsg = LogHelper.getStackTraceFirstLine(ex);
			}
		}
	}
}
